// packageF/cps/detail/detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dy_password: "",
    dy_deeplink:"",
    
    goods_id: "",
    detail: {},
    show1: false,
    customer_open: false,

    tiktok_popup: false,
    indicator_dots: false,

    posterShow: false,
    language:"",
    current: 1,
    xqtop:0,
    hoet:false,
    show_dy_password:false,
  },
  close_show_dy(){
    this.setData({show_dy_password:false})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en,
    });
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(',');
        this.setData({goods_id:info_arr[0]})//因抖音cps的商品id长度过长，与后端协商，建商品id和mid通过,拼接，第二个固定为mid
        app._setMid(info_arr[1])
      }
    }
    if(options.goods_id){

      this.setData({
        goods_id: options.goods_id 
      });
    }
    this.getData();

  },
  onPageScroll(e) {
    let scrollTop = e.scrollTop;
    if (scrollTop > 35) {
      if (!this.data.hoet) {
        this.setData({
          hoet: true,
        });
      }
    } else {
      if (this.data.hoet) {
        this.setData({
          hoet: false,
        });
      }
    }
    this.setData({
      datop: scrollTop,
    });
  },
  touchStart(e) {
    let sy = e.touches[0].pageY;
    this.setData({
      sy: sy,
    });
  },
  touchMove(e) {
    this.setData({
      endy: e.touches[0].pageY,
    });
  },
  touchEnd() {
    if (this.data.sy - this.data.endy > 100 && this.data.showIn) {
      this.setData({
        boxHeight: "",
        xqBoxShow: true,
        showIn: false,
      });
    }
  },
  getTap(e) {
    let pid = e.currentTarget.id;
    this.setData({
      current: pid,
    });
    if (pid == 1) {
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300,
      });
    } else if (pid == 2) {
      wx.pageScrollTo({
        scrollTop: this.data.pjtop,
        duration: 300,
      });
    } else if (pid == 3) {
      this.setData({
        xqBoxShow: true,
      });
      wx.pageScrollTo({
        scrollTop: this.data.xqtop + 400,
        duration: 300,
      });
    }
  },
  closePop() {
    this.setData({
      show1: false,
    });
  },
  // 跳转主页
  gotoHome() {
    wx.reLaunch({
      url: "/packageF/cps/index/index",
    });
  },
  gotoMember() {
    wx.reLaunch({
      url: "/packageG/member_v2/member_v2",
    });
  },
  gotoChat(evt){
    let url = this.data.detail.cservice.cservice+ '&goods_id='+this.data.goods_id;
    wx.navigateTo({
      url: url,
      fail: (err) => {
        app.tips("客服路由出错");
      }
    });
  },
  showPop() {
    let setServicePlugin = app._isTextEmpty(this.data.detail.cservice.service_mobile) && app._isTextEmpty(this.data.detail.cservice.service_QRcode);
    if (app._isTextEmpty(this.data.detail.cservice) && setServicePlugin) {
      return app.tips("抱歉，暂未设置客服信息");
    }
    if (!app._isTextEmpty(this.data.detail.cservice) && setServicePlugin) {
      this.gotoChat({
        currentTarget: {
          dataset: {
            cservice: this.data.detail.cservice.cservice
          }
        }
      }) //只设置了客服链接，直接跳转
      return;
    }
    this.setData({
      show1: true,
    });
  },
  setPosterShow() {
    this.setData({
      posterShow: !this.data.posterShow,
      tiktok_popup: false
    });
  },
  ShowTiktokPoupup() {
    this.setData({
      tiktok_popup: true
    })
  },
  show_dialog(){
    this.setData({show_dy_password:true,tiktok_popup:false})
  },
  code_link() {
    wx.setClipboardData({
      data: this.data.dy_password,
      success: () => {
        app.tips('口令已复制，打开抖音app下单！')
        this.setData({
          tiktok_popup: false
        })
      },
      fail: (err) => {
        if(this.detail.detail_url){
          this.getTikTokCps(this.detail.detail_url)
        }
        // app.tips('复制失败')
      }
    });
  },
  gotoindex() {
    wx.navigateTo({
      url: '/packageG/index/index'
    });
  },
  toGroupCode() {
    if (this.data.detail.group_code.jump_mode === 'home') {
      wx.reLaunch({
        url: '/packageE/groupCode/groupCode',
      });
    } else if (this.data.detail.group_code.code_mini_url) {
      wx.navigateTo({
        url: this.data.detail.group_code.code_mini_url
      });
    }
  },
  getTikTokCps(detail_url) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.tiktok-cps.api.goods.tranfer-link'),
      data: {detail_url},
      success: resdata => {
        let {
          data,
          result
        } = resdata.data;
        if (result == 0) {
          return 
        }
        this.setData({
          dy_password: data.dy_password,
          dy_deeplink:data.dy_deeplink
        })
      }
    })

  },
  onClose(){
    this.setData({tiktok_popup:false})
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.tiktok-cps.api.goods.good-detail'),
      data: {
        goods_id: this.data.goods_id
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result == 0) {
          return app.tips(msg)
        }

        if (data.imgs && data.imgs.length > 1) {
          this.setData({
            indicator_dots: true
          })
        }
        this.setData({
          detail: data || {}
        })
        this.getTikTokCps(data.detail_url)
      }
    })
  },
  previewImg(e) {
    let ind;
    ind = e.currentTarget.dataset.key;
    wx.previewImage({
      current: ind ? this.data.detail.imgs[ind] : this.data.detail.imgs, // 当前显示图片的http链接
      urls: this.data.detail.imgs // 需要预览的图片http链接列表
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.createSelectorQuery()
    .select(".foShowBox")
    .boundingClientRect(rect=> {
      this.setData({
        xqtop: rect.top - 100,
      });
    })
    .exec();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path:`/packageF/cps/detail/detail?goods_id=${this.data.goods_id}`
    }
  }
})